package com.neusoft.elmboot.service.impl;


import java.math.BigDecimal;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.enums.transStatus;
import com.neusoft.elmboot.mapper.VirtualWalletMapper;
import com.neusoft.elmboot.mapper.VirtualWalletTransMapper;
import com.neusoft.elmboot.po.VirtualWallet;
import com.neusoft.elmboot.po.VirtualWalletTrans;
import com.neusoft.elmboot.service.VirtualWalletService;


@Service
// 定义了一个虚拟钱包服务类，用于处理钱包相关的业务逻辑
public class VirtualWalletServiceImpl implements VirtualWalletService{

    @Autowired
    private VirtualWalletMapper virtualwalletMapper;

    @Autowired
    private VirtualWalletTransMapper virtualWalletTransMapper ;
    

    @Override
    public int createVirtualWallet(String userId) {
        // 创建一个新的虚拟钱包实例
        VirtualWallet newWallet = new VirtualWallet(userId);
        return virtualwalletMapper.createVirtualWallet(newWallet);

    }

    //根据用户ID获取虚拟钱包对象
    @Override
    public VirtualWallet getVirtualWalletByUserId(String userId) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWalletByUserId(userId);
        // 返回虚拟钱包对象
        return wallet;
    }

    // 根据钱包ID获取虚拟钱包对象
    @Override
    public VirtualWallet getVirtualWallet(Long walletId) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 返回虚拟钱包对象
        return wallet;
    }

    // 根据钱包ID获取钱包余额
    @Override
    public BigDecimal getBalance(Long walletId) {
        // 从仓库中获取钱包余额
        return virtualwalletMapper.getBalance(walletId);
    }

    // 从钱包中扣除指定金额
    @Override
    public void debit(Long walletId, BigDecimal amount) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 调用虚拟钱包对象的debit方法扣除金额
        wallet.debit(amount);
        // 更新仓库中的钱包余额
        virtualwalletMapper.updateBalance(walletId, wallet.balance());
    }

    // 向钱包中增加指定金额
    @Override
    public void credit(Long walletId, BigDecimal amount) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 调用虚拟钱包对象的credit方法增加金额
        wallet.credit(amount);
        // 更新仓库中的钱包余额
        virtualwalletMapper.updateBalance(walletId, wallet.balance());
    }

    // 向钱包中增加透支金额
    @Override
    public void increaseOverdraftAmount(Long walletId, BigDecimal amount) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 调用虚拟钱包对象的credit方法增加金额
        wallet.increaseOverdraftAmount(amount);
        // 更新仓库中的钱包余额
        virtualwalletMapper.updateOverdraftAmount(walletId, wallet.OverdraftAmount());
    }

    //调用虚拟钱包对象的credit方法减少已透支金额
    @Override
    public void decreaseOverdraftAmount(Long walletId, BigDecimal amount) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 调用虚拟钱包对象的credit方法减少已透支金额
        wallet.decreaseOverdraftAmount(amount);
        // 更新仓库中的钱包余额
        virtualwalletMapper.updateOverdraftAmount(walletId, wallet.OverdraftAmount());
    }

    //调用虚拟钱包对象的credit方法增加已透支金额
    @Override
    public void increaseOverdraftAmount(Long walletId, BigDecimal amount,Integer repaymentDeadline){
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 调用虚拟钱包对象的calculateInterest方法计算利息
        wallet.increaseOverdraftAmount(amount);
        wallet.increaseOverdraftAmount( wallet.calculateInterest(amount,repaymentDeadline));
        // 更新仓库中的钱包余额
        virtualwalletMapper.updateOverdraftAmount(walletId, wallet.OverdraftAmount());
    }

    //解冻
    @Override
    public void unfreeze(Long walletId, BigDecimal amount) {
        // 从仓库中获取钱包实体
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        // 调用虚拟钱包对象的unfreeze方法解冻金额
        wallet.unfreeze(amount);
        // 更新仓库中的钱包余额
        virtualwalletMapper.updateBalance(walletId, wallet.balance());
    }

    @Override
    // 实现钱包之间的转账操作
    public void transfer(Long fromWalletId, Long toWalletId, BigDecimal amount) {
        // 创建一个新的交易实体
        VirtualWalletTrans walletTrans = new VirtualWalletTrans();
        // 设置交易金额
        walletTrans.setAmount(amount);
        walletTrans.setFromWalletId(fromWalletId);
        walletTrans.setToWalletId(toWalletId);
        // 保存交易实体并获取交易ID
        Long transactionId = virtualWalletTransMapper.saveTransaction(walletTrans);
        System.out.println("交易ID:"+transactionId);
        try {
            // 从源钱包扣除金额
            
            debit(fromWalletId, amount);
    
        } catch (IllegalArgumentException e) {
            // 如果余额不足，更新交易状态为关闭，并重新抛出异常
            virtualWalletTransMapper.updateStatus(transactionId, transStatus.CLOSED);
        } catch (Exception e) {
            // 如果发生其他异常，更新交易状态为失败，并重新抛出异常
            virtualWalletTransMapper.updateStatus(transactionId, transStatus.FAILED);
        }
        // 如果用户转账未收货，更新交易状态为执行中
        virtualWalletTransMapper.updateStatus(transactionId, transStatus.EXECUTING);
    }

    



    @Override
    // 实现钱包之间的转账操作
    public void transfer(Long fromWalletId, Long toWalletId, BigDecimal amount,Integer repaymentDeadline) {
        // 创建一个新的交易实体
        VirtualWalletTrans walletTrans = new VirtualWalletTrans();
        // 设置交易金额
        walletTrans.setAmount(amount);
        walletTrans.setFromWalletId(fromWalletId);
        walletTrans.setToWalletId(toWalletId);
        // 保存交易实体并获取交易ID
        Long transactionId = virtualWalletTransMapper.saveTransaction(walletTrans);
        System.out.println("交易ID:"+transactionId);
        try {
            // 从源钱包扣除金额
            increaseOverdraftAmount(fromWalletId, amount,repaymentDeadline);
            
        } catch (IllegalArgumentException e) {
            // 如果余额不足，更新交易状态为关闭，并重新抛出异常
            virtualWalletTransMapper.updateStatus(transactionId, transStatus.CLOSED);
        } catch (Exception e) {
            // 如果发生其他异常，更新交易状态为失败，并重新抛出异常
            virtualWalletTransMapper.updateStatus(transactionId, transStatus.FAILED);
        }
        // 如果用户转账未收货，更新交易状态为执行中
        virtualWalletTransMapper.updateStatus(transactionId, transStatus.EXECUTING);
    }

    @Override
    //解冻,确认收货
    public void confirm(Long transactionId) {
        // 从仓库中获取交易实体
        VirtualWalletTrans walletTrans=virtualWalletTransMapper.getTransaction(transactionId);
        //如果交易状态为执行中，则解冻金额并更新交易状态为已执行
        if (walletTrans.getStatus().equals(transStatus.EXECUTING)){
            BigDecimal amount=walletTrans.getAmount();
            unfreeze(walletTrans.getToWalletId(),amount);
            virtualWalletTransMapper.updateStatus(transactionId, transStatus.EXECUTED);
        }
        //如果交易状态不为执行中，则更新交易状态为失败
        else {
            virtualWalletTransMapper.updateStatus(transactionId, transStatus.FAILED);
        }
    }

    @Override
    //充值
    public void deposit(Long walletId, BigDecimal amount) {
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        wallet.deposit(amount);
        virtualwalletMapper.updateBalance(walletId, wallet.balance());
    }

    @Override
    //提现
    public void withdraw(Long walletId, BigDecimal amount) {
        VirtualWallet wallet = virtualwalletMapper.getVirtualWallet(walletId);
        wallet.withdraw(amount);
        virtualwalletMapper.updateBalance(walletId, wallet.balance());
    }

    //列举用户的交易流水
    @Override
    public List<VirtualWalletTrans> listTransactions(Long walletId) {
        return virtualWalletTransMapper.listTransactions(walletId);
    }

}